<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddAllForeignToAllTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('categories', function(Blueprint $table)
        {
            $table->foreign('parent_id')->references('id')->on('categories')->onUpdate('CASCADE')->onDelete('SET NULL');
        });

        Schema::table('comments', function(Blueprint $table)
        {
            $table->foreign('parent_id')->references('id')->on('comments')->onUpdate('CASCADE')->onDelete('SET NULL');
            $table->foreign('post_id')->references('id')->on('posts')->onUpdate('CASCADE')->onDelete('SET NULL');
        });
        Schema::table('post_tag', function(Blueprint $table)
        {
            $table->foreign('post_id')->references('id')->on('posts')->onUpdate('CASCADE')->onDelete('SET NULL');
            $table->foreign('tag_id')->references('id')->on('tags')->onUpdate('CASCADE')->onDelete('SET NULL');
        });

        Schema::table('posts', function(Blueprint $table)
        {
            $table->foreign('category_id')->references('id')->on('categories')->onUpdate('CASCADE')->onDelete('SET NULL');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
//        DB::update(" ALTER TABLE `categories` DROP FOREIGN KEY `categories_parent_id_foreign`; ");
//        DB::update(" ALTER TABLE `posts` DROP FOREIGN KEY `posts_category_id_foreign`; ");
//        DB::update(" ALTER TABLE `comments` DROP FOREIGN KEY `comments_parent_id_foreign`; ");
//        DB::update(" ALTER TABLE `comments` DROP FOREIGN KEY `comments_post_id_foreign`; ");
//        DB::update(" ALTER TABLE `post_tag` DROP FOREIGN KEY `post_tag_post_id_foreign`; ");
//        DB::update(" ALTER TABLE `post_tag` DROP FOREIGN KEY `post_tag_tag_id_foreign`; ");

        Schema::table('categories', function(Blueprint $table)
        {
            $table->dropForeign('categories_parent_id_foreign');
        });
        Schema::table('comments', function(Blueprint $table)
        {
            $table->dropForeign('comments_parent_id_foreign');
            $table->dropForeign('comments_post_id_foreign');
        });

        Schema::table('post_tag', function(Blueprint $table)
        {
            $table->dropForeign('post_tag_post_id_foreign');
            $table->dropForeign('post_tag_tag_id_foreign');
        });

        Schema::table('posts', function(Blueprint $table)
        {
            $table->dropForeign('posts_category_id_foreign');
        });
    }
}
